unit mymsgs;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, SyncObjs;

type
  Tmymsg = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure FormCanResize(Sender: TObject; var NewWidth,
      NewHeight: Integer; var Resize: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    CritSect:TCriticalSection;
    constructor ActualCreate;
  public
    { Public declarations }
    procedure ShowMyMsg(S:String);
    constructor Create(AOwner: TComponent); override;
    class function GetInstance: Tmymsg;
    procedure ReleaseInstance;
  end;

var
    Ref_Count:Word=0;
    Instance:TMyMsg = nil;

implementation
{$R *.dfm}

constructor Tmymsg.ActualCreate;
begin
    inherited Create(nil);
end;

constructor Tmymsg.Create(AOwner: TComponent);
begin
    raise Exception.Create('Attempt to create an instance of Tmymsg')
end;

class function Tmymsg.GetInstance: Tmymsg;
begin
    if (Instance = nil) then
    begin
        Instance := Tmymsg.ActualCreate;
    end;
    Inc(Ref_Count);
    Result := Instance;
end;

procedure Tmymsg.ShowMyMsg(S:String);
begin
    CritSect.Enter;
    try
        if not(Visible) then
        begin
            memo1.Lines.Clear;
            Visible:=True;
        end;
        memo1.Lines.Add(S);
    finally
        CritSect.Leave;
    end;
end;

procedure Tmymsg.FormCanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
begin
    Resize:=False;
end;

procedure Tmymsg.Button1Click(Sender: TObject);
begin
    Visible:=False;
end;

procedure Tmymsg.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
    CanClose:=False;
    Visible:=False;
end;

procedure Tmymsg.FormDestroy(Sender: TObject);
begin
    CritSect.Free;
    Instance:=nil;
    inherited;
end;

procedure Tmymsg.ReleaseInstance;
begin
    Dec(Ref_Count);
    if (Ref_Count=0) then
    begin
//        FormDestroy(nil);
        FreeAndNil(Instance);
    end;
end;

procedure Tmymsg.FormCreate(Sender: TObject);
begin
    CritSect:=TCriticalSection.Create;
end;

end.
